package org.example.statistics.service.impl;

import java.util.*;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import org.example.statistics.domain.StatisticsSell;
import org.example.statistics.mapper.StatisticsBuyMapper;
import org.example.statistics.mapper.StatisticsSellMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.example.statistics.mapper.StatisticsMonthMoneyMapper;
import org.example.statistics.domain.StatisticsMonthMoney;
import org.example.statistics.service.IStatisticsMonthMoneyService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 统计月份报Service业务层处理
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-05-18
 */
@Service
public class StatisticsMonthMoneyServiceImpl implements IStatisticsMonthMoneyService
{
    @Autowired
    private StatisticsMonthMoneyMapper statisticsMonthMoneyMapper;

    @Autowired
    private StatisticsBuyMapper statisticsBuyMapper;

    @Autowired
    private StatisticsSellMapper statisticsSellMapper;

    /**
     * 查询统计月份报
     *
     * @param statisticsId 统计月份报主键
     * @return 统计月份报
     */
    @Override
    public StatisticsMonthMoney selectStatisticsMonthMoneyByStatisticsId(Long statisticsId)
    {
        return statisticsMonthMoneyMapper.selectStatisticsMonthMoneyByStatisticsId(statisticsId);
    }

    /**
     * 查询统计月份报列表
     *
     * @param statisticsMonthMoney 统计月份报
     * @return 统计月份报
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<StatisticsMonthMoney> selectStatisticsMonthMoneyList(StatisticsMonthMoney statisticsMonthMoney)
    {
        return statisticsMonthMoneyMapper.selectStatisticsMonthMoneyList(statisticsMonthMoney);
    }



    /**
     * 新增统计月份报
     *
     * @param statisticsMonthMoney 统计月份报
     * @return 结果
     */
    @Override
    public int insertStatisticsMonthMoney(StatisticsMonthMoney statisticsMonthMoney)
    {

        statisticsMonthMoney.setCreateTime(DateUtils.getNowDate());
        return statisticsMonthMoneyMapper.insertStatisticsMonthMoney(statisticsMonthMoney);
    }

    /**
     * 修改统计月份报
     *
     * @param statisticsMonthMoney 统计月份报
     * @return 结果
     */
    @Override
    public int updateStatisticsMonthMoney(StatisticsMonthMoney statisticsMonthMoney)
    {
        return statisticsMonthMoneyMapper.updateStatisticsMonthMoney(statisticsMonthMoney);
    }

    /**
     * 批量删除统计月份报
     *
     * @param statisticsIds 需要删除的统计月份报主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsMonthMoneyByStatisticsIds(String statisticsIds)
    {
        return statisticsMonthMoneyMapper.deleteStatisticsMonthMoneyByStatisticsIds(Convert.toStrArray(statisticsIds));
    }

    /**
     * 删除统计月份报信息
     *
     * @param statisticsId 统计月份报主键
     * @return 结果
     */
    @Override
    public int deleteStatisticsMonthMoneyByStatisticsId(Long statisticsId)
    {
        return statisticsMonthMoneyMapper.deleteStatisticsMonthMoneyByStatisticsId(statisticsId);
    }



    //任务调度器统计
    @Override
    @Transactional
    public int insertTaskStatisticsMonthMoney(StatisticsMonthMoney statisticsMonthMoney) {

        // 获取当前日期的Calendar实例 将当前日期的月份减1
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        statisticsMonthMoney.setCreateTime( calendar.getTime());

        List<StatisticsMonthMoney> inList = statisticsMonthMoneyMapper.selectContractList(statisticsMonthMoney);
        statisticsMonthMoneyMapper.batchInsert(inList);


        List<StatisticsMonthMoney> upList = statisticsMonthMoneyMapper.selectStatisticsContractByMonth(statisticsMonthMoney);
        statisticsMonthMoneyMapper.batchUpdate(upList);


        return 1;
    }

}
